$(function(){

	var userInfoUrl = "/cms/appUser/avatarService/edit";
	var userInfoSaveUrl = "/cms/appUser/avatarService/edit";
	var $curBtn = null;
	var $curRow = null;
	var curUserInfo = null;
	var curUid = null;

	$('.pic').each(function(){
		var $this = $(this);
		if($this.attr('style') != 'background-image:url()' ) {
			$this.children('a').show();
		}
	});

	var $photomodalEle =$('#photo-edit-modal');		
	$photomodalEle.modal({
		backdrop: 'static',
		show: false
	});

	function handleUploadSuccess(obj) {
		var $this = this;
		var photoUrl = obj['data'];
		$this.siblings('.pic').css('background-image','url('+photoUrl+')');	
		$this.parent().find('[download]').attr('href',photoUrl);
	}

	var uploader = new PicUploader({
		before: function() {
			$curBtn.siblings('.box').find('.overlay').show();
		},
		success: function(obj){
			handleUploadSuccess.call($curBtn,obj);
			$('.btn.upload').removeAttr('disabled');
			$curBtn.siblings('.box').find('.overlay').hide();
		}
	});

	$('.btn.upload').on('click',function(){
		$('.btn.upload').attr('disabled','');
		uploader.start();
		$curBtn = $(this);
	});

	function handleUserInfoBack(obj){
		var data = JSON.parse(obj['data']);
//		var data = obj['data'];
		var bigPhoUrl = data['show_url'];
		var smPhoUrl = data['head_url'];
		var desc = data['introduce'];
		if(bigPhoUrl !== ""){
			$photomodalEle.find('.pic.lg').css('background-image','url('+bigPhoUrl+')');
		}
	
		if(smPhoUrl !== "") {
			$photomodalEle.find('.pic.sm').css('background-image','url('+smPhoUrl+')');	
		}
		
		$photomodalEle.find('textarea').val(desc);
		$photomodalEle.modal('show');
	}

	function getUersInfo(callback) {
		curUid = $(this).parents('tr').attr('data-user-id');
		id = $(this).parents('tr').attr('data-id');
		cAjax({
			url: userInfoUrl,
			type: 'get',
			data: {
				uid: curUid
			},
			success: function(obj) {
				curUserInfo = obj['data'];
				callback(obj);
				
			}
		});	
	}

	$('.edit-btn').on('click',function(){
		$curRow = $(this).parents('tr');
		getUersInfo.call(this,handleUserInfoBack);
	});	

	$('.save-btn').on('click',function(){
		function handleSuccess() {
			$photomodalEle.modal('hide');
			$curRow.find('.unprocessed').hide();
			$curRow.find('.processed').show();
			greeting({
				msg: '保存成功',
				confirm:function(){
					window.location.reload();
				}
			});	
		}
		
		var showUrl = $photomodalEle.find('.pic.lg').css('background-image');
		showUrl = showUrl.substring(4,showUrl.length-1);
		if(showUrl.startsWith("\"")&&showUrl.endsWith("\"")){
			showUrl = showUrl.substring(1,showUrl.length-1);
		}
		
		var headUrl = $photomodalEle.find('.pic.sm').css('background-image');	
		headUrl = headUrl.substring(4,headUrl.length-1);
		if(headUrl.startsWith("\"")&&headUrl.endsWith("\"")){
			headUrl = headUrl.substring(1,headUrl.length-1);
		}
		cAjax({
			url: userInfoSaveUrl,
			data: {
				id: id,
				user_id: curUid,
				show_url: showUrl,
				head_url: headUrl,
				introduce: $photomodalEle.find('textarea').val()
			},
			type: 'post',
			success: function(obj) {
				handleSuccess();
			}
		});
		
	});

});
